package com.funny.learning.english.activity;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.funny.learning.english.GameBaseActivity;
import com.funny.learning.english.R;
import com.funny.learning.english.model.GameEntity;
import com.funny.learning.english.model.HistoryEntity;

public class TimHinhAnhActivity extends GameBaseActivity implements OnClickListener{
	
	private String TAG = "TimHinhAnhActivity";
	
	private RelativeLayout rl1,rl2,rl3,rl4;
	private TextView tv1,tv2,tv3,tv4;
	private ImageView iv1,iv2,iv3,iv4;
	
	private int countTrue = 0;
	
	private int posDung = -1;
	
	private int srcSound = 0;
	
//	private ArrayList<LangNgheEntity> arrData = new ArrayList<LangNgheEntity>();
	private TextView tvCauHoi; 
	
	private GameEntity gameEntity1;
	private GameEntity gameEntity2;
	private GameEntity gameEntity3;
	private GameEntity gameEntity4;
	private GameEntity gameEntityDapAn;
	
	private ArrayList<GameEntity> arrGameEntities;

	private Animation animation1;
	private boolean isPause = false;

	private ImageView ivPause;

	private ImageView ivPlay;

	private ImageView ivRefresh;

	private ImageView ivBack;

	private LinearLayout llPause;

	private TextView tvTitle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.tim_hinh_anh_layout);
		super.onCreate(savedInstanceState);
		
		ivPause = (ImageView)findViewById(R.id.ivPause);
		ivPlay = (ImageView)findViewById(R.id.ivPlay);
		ivRefresh = (ImageView)findViewById(R.id.ivRefresh);
		ivBack = (ImageView)findViewById(R.id.ivBack);
		llPause = (LinearLayout)findViewById(R.id.llPause);
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		
		ivBack.setOnClickListener(this);
		ivPause.setOnClickListener(this);
		ivPlay.setOnClickListener(this);
		ivRefresh.setOnClickListener(this);
		
	}
	
	private void checkDapAn(final Animation animation, final RelativeLayout relativeLayout, final TextView textView, final ImageView imageView){
		relativeLayout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String str = textView.getText().toString();
				if(str.equals(gameEntityDapAn.getLname())){
//					imageView.setImageResource(R.drawable.image1);
					imageView.setVisibility(View.VISIBLE);
					textView.setVisibility(View.GONE);
					
					playMusic(getPathSound(gameEntityDapAn));
					
					relativeLayout.startAnimation(animation);
					
					animation.setAnimationListener(new AnimationListener() {
						
						@Override
						public void onAnimationStart(Animation arg0) {
							
						}
						
						@Override
						public void onAnimationRepeat(Animation arg0) {
							
						}
						
						@Override
						public void onAnimationEnd(Animation arg0) {
							if(countCheckScreen < countTrue){
								countCheckScreen = countCheckScreen + 1;
								countAnwserCorrect = countAnwserCorrect + 1;
							}
							countTrue = countTrue + 1;
							if(countTrue >= mGameEntities.size()){
								finishGame();
								HistoryEntity historyEntity = new HistoryEntity(1, thumbTopic, slugGameName, countAnwserCorrect, mGameEntities.size(), (int) System.currentTimeMillis(), slugTopicName, slugSubTopicName);
								mHistoryDAO.saveHistory(historyEntity);
							}else{
								arrGameEntities.clear();
								refreshView();
								intitData(countTrue);
							}
						}
					});
				}else{
					if(countCheckScreen < countTrue){
						countCheckScreen = countCheckScreen + 1;
					}
					relativeLayout.setBackgroundResource(R.drawable.chon_tu_sai);
				}
			}
		});
	}
	
	private void refreshView(){
		rl1.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		rl2.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		rl3.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		rl4.setBackgroundResource(R.drawable.stroke_img_topic_tim_hinh_anh);
		
		
		tv1.setVisibility(View.VISIBLE);
		tv2.setVisibility(View.VISIBLE);
		tv3.setVisibility(View.VISIBLE);
		tv4.setVisibility(View.VISIBLE);
		
		tv1.setText("");
		tv2.setText("");
		tv3.setText("");
		tv4.setText("");
		
		iv1.setVisibility(View.GONE);
		iv2.setVisibility(View.GONE);
		iv3.setVisibility(View.GONE);
		iv4.setVisibility(View.GONE);
		
		gameEntity1 = null;
		gameEntity2 = null;
		gameEntity3 = null;
		gameEntity4 = null;
		gameEntityDapAn = null;
	}
	
	 @Override
	protected void initSucess() {
		// TODO Auto-generated method stub
		super.initSucess();
		setTitle("Tìm hình ảnh");
		
		animation1 = AnimationUtils.loadAnimation(TimHinhAnhActivity.this, R.anim.g_mw_correct);
		
		rl1 = (RelativeLayout)findViewById(R.id.rl1);
		rl2 = (RelativeLayout)findViewById(R.id.rl2);
		rl3 = (RelativeLayout)findViewById(R.id.rl3);
		rl4 = (RelativeLayout)findViewById(R.id.rl4);
		
		tv1 = (TextView)findViewById(R.id.tv1);
		tv2 = (TextView)findViewById(R.id.tv2);
		tv3 = (TextView)findViewById(R.id.tv3);
		tv4 = (TextView)findViewById(R.id.tv4);
		tvCauHoi = (TextView)findViewById(R.id.tvCauHoi);
		
		iv1 = (ImageView)findViewById(R.id.iv1);
		iv2 = (ImageView)findViewById(R.id.iv2);
		iv3 = (ImageView)findViewById(R.id.iv3);
		iv4 = (ImageView)findViewById(R.id.iv4);
		
		Toast.makeText(activity, "size: " + mGameEntities.size(), 2000).show();
		
		intitData(countTrue);
	}
	 
	 private void intitData(int count){
		 refreshView();
		
		 arrGameEntities = new ArrayList<GameEntity>();
		 
		 GameEntity gameEntity = mGameEntities.get(count);
		 tvCauHoi.setText(gameEntity.getName());
		 
		 Log.d(TAG, "gameEntity::" + gameEntity.getLname());
		 
		 gameEntityDapAn = gameEntity;
		 int countRandom = getNumberRandom();
		 if(countRandom == 1){
			 gameEntity1 = gameEntityDapAn;
			 
			if (gameEntity1.getLname() == null) {
				countTrue = countTrue + 1;
				if(countTrue >= mGameEntities.size()){
					finishGame();
				}else{
					intitData(countTrue);
				}
				
			} else {
				arrGameEntities.add(gameEntity1);
				 while (gameEntity2 == null) {
					 gameEntity2 = getRandomGameEntity();
				 }
				 while (gameEntity3 == null) {
					 gameEntity3 = getRandomGameEntity();
				 }
				 while (gameEntity4 == null) {
					 gameEntity4 = getRandomGameEntity();
				 }
			}
			 
			 
		 }else if(countRandom == 2){
			 gameEntity2 = gameEntityDapAn;
			 
			 if (gameEntity2.getLname() == null) {
					countTrue = countTrue + 1;
					if(countTrue >= mGameEntities.size()){
						finishGame();
					}else{
						intitData(countTrue);
					}
					
				} else {
					arrGameEntities.add(gameEntity2);
					 while (gameEntity1 == null) {
						 gameEntity1 = getRandomGameEntity();
					 }
					 while (gameEntity3 == null) {
						 gameEntity3 = getRandomGameEntity();
					 }
					 while (gameEntity4 == null) {
						 gameEntity4 = getRandomGameEntity();
					 }
				}
			 
			
		 }else if(countRandom == 3){
			 gameEntity3 = gameEntityDapAn;
			 
			 if (gameEntity3.getLname() == null) {
					countTrue = countTrue + 1;
					if(countTrue >= mGameEntities.size()){
						finishGame();
					}else{
						intitData(countTrue);
					}
					
				} else {
					arrGameEntities.add(gameEntity3);
					 
					 while (gameEntity1 == null) {
						 gameEntity1 = getRandomGameEntity();
					 }
					 while (gameEntity2 == null) {
						 gameEntity2 = getRandomGameEntity();
					 }
					 while (gameEntity4 == null) {
						 gameEntity4 = getRandomGameEntity();
					 }
				}
			 
		 }else{
			 gameEntity4 = gameEntityDapAn;
			 
			 if (gameEntity4.getLname() == null) {
					countTrue = countTrue + 1;
					if(countTrue >= mGameEntities.size()){
						finishGame();
					}else{
						intitData(countTrue);
					}
					
				} else {
					 arrGameEntities.add(gameEntity4);
					 
					 while (gameEntity1 == null) {
						 gameEntity1 = getRandomGameEntity();
					 }
					 while (gameEntity3 == null) {
						 gameEntity3 = getRandomGameEntity();
					 }
					 while (gameEntity2 == null) {
						 gameEntity2 = getRandomGameEntity();
					 }
				}
			
		 }
		 
//		Log.d(TAG, "tv1:" + gameEntity1.getLname());
//		Log.d(TAG, "tv2:" + gameEntity2.getLname());
//		Log.d(TAG, "tv3:" + gameEntity3.getLname());
//		Log.d(TAG, "tv4:" + gameEntity4.getLname());
		
		tv1.setText(gameEntity1.getLname());
		tv2.setText(gameEntity2.getLname());
		tv3.setText(gameEntity3.getLname());
		tv4.setText(gameEntity4.getLname());
		
		 
		Bitmap bm1 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity1)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		Bitmap bm2 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity2)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		Bitmap bm3 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity3)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		Bitmap bm4 = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(getPathImg(gameEntity4)),getResources().getDimensionPixelSize(R.dimen.game_img_small), getResources().getDimensionPixelSize(R.dimen.game_img_small), true);
		 
		iv1.setImageBitmap(bm1);
		iv2.setImageBitmap(bm2);
		iv3.setImageBitmap(bm3);
		iv4.setImageBitmap(bm4);
		
//		Log.d(TAG, "getPathImg1:" + getPathImg(gameEntity1));
//		Log.d(TAG, "getPathImg2:" + getPathImg(gameEntity2));
//		Log.d(TAG, "getPathImg3:" + getPathImg(gameEntity3));
//		Log.d(TAG, "getPathImg4:" + getPathImg(gameEntity4));
		
		checkDapAn(animation1, rl1, tv1, iv1);
		checkDapAn(animation1, rl2, tv2, iv2);
		checkDapAn(animation1, rl3, tv3, iv3);
		checkDapAn(animation1, rl4, tv4, iv4);
		 
	 }
	 
	 private int getNumberRandom(){
			Random ran = new Random();
			int x = ran.nextInt(4);
			
			Log.d(TAG, "number:" + x);
			
			return x;
		}
		
		private GameEntity getRandomGameEntity(){
			GameEntity gameEntity = mGameEntities.get(new Random().nextInt(mGameEntities.size()));
			
//			Log.d(TAG, "getRandomGameEntity gameEntity::" + gameEntity.getLname());
			if(gameEntity.getLname() == null || gameEntity.getLname().equals("")){
				return null;
			}else{
				if(!arrGameEntities.contains(gameEntity)){
					arrGameEntities.add(gameEntity);
					return gameEntity;
				}else{
//					getRandomGameEntity();
					return null;
				}
			}
			
		}
		
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.ivPause:
				isPause = !isPause;
				if(isPause){
					ivPause.setImageResource(R.drawable.ic_play);
					llPause.setVisibility(View.VISIBLE);
				}else{
					ivPause.setImageResource(R.drawable.ic_pause);
					llPause.setVisibility(View.GONE);
				}
				break;
			case R.id.ivPlay:
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
				break;
			case R.id.ivRefresh:
				ivPause.setImageResource(R.drawable.ic_pause);
				llPause.setVisibility(View.GONE);
				countTrue = 0;
				intitData(countTrue);
				break;
			case R.id.ivBack:
				onBackPressed();
				break;

			default:
				break;
			}
		}
	
}
